<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/global.jsp"%>

<table id="categoryList" style="height: 100%; width: 100%;">

</table>

<div id="categoryListToolBars" style="height: auto">
	<a href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-remove',plain:true"
		onclick="categoryList_remove()">删除</a>| <a href="javascript:void(0)"
		class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"
		onclick="categoryList_append()">添加</a>|<a href="javascript:void(0)"
		class="easyui-linkbutton"
		data-options="iconCls:'icon-edit',plain:true"
		onclick="categoryList_edit()">编辑</a> | <a href="javascript:void(0)"
		class="easyui-linkbutton"
		data-options="iconCls:'icon-undo',plain:true"
		onclick="categoryList_undo()">取消</a>| <a href="javascript:void(0)"
		class="easyui-linkbutton"
		data-options="iconCls:'icon-save',plain:true"
		onclick="categoryList_accept()">保存</a>| <input
		id="categoryItemTypeQue" style="width: 100px;" /> |<input
		id="categoryckeyQue" style="width: 100px;" />|<input
		id="categoryName" style="width: 200px;" />
</div>
<script type="text/javascript">
	var categoryQueryParams = {
		itemType : 0,
		ckey : '',
		entity : true,
		like_name : ''
	};
	function categoryListDatagridReload() {
		try {
			categoryQueryParams.itemType = categoryItemTypeQue
					.combobox('getValue');
			categoryQueryParams.ckey = categoryckeyQue.combobox('getValue');
			categoryQueryParams.like_name = categoryName.textbox('getValue');

			categoryListDatagrid.datagrid('reload');
		} catch (e) {
		}
	}
</script>
<script type="text/javascript">
	var categoryckeyQue = undefined;
	var categoryItemTypeQue = undefined;
	var categoryName = undefined;

	var categorySelectIndex = undefined;
	var categorySelectRow = undefined;
	var categoryEditIndex = undefined;
	var categoryEditRow = undefined;
	var categoryListDatagrid = undefined;
	$(function() {

		categoryName = $('#categoryName').textbox({
			buttonText : '名称搜索',
			buttonIcon : 'icon-search',
			buttonAlign : 'right'
		});

		categoryName.textbox('button').click(function() {
			// 			if (!categoryName.textbox('getValue')) {
			// 				return;
			// 			}
			categoryListDatagridReload();
		});

		categoryckeyQue = $('#categoryckeyQue').combobox({
			onChange : function(newValue, oldValue) {
				categoryListDatagridReload();
			},
			data : [ {
				ckey : '',
				text : '全部分类集合',
				selected : true
			} ],
			valueField : 'ckey',
			textField : 'text'
		});

		categoryItemTypeQue = $('#categoryItemTypeQue').combobox(
				{
					valueField : 'id',
					textField : 'text',
					data : [ {
						"id" : 0,
						"text" : "全部资源类型",
						"selected" : true
					}, {
						"id" : 1,
						"text" : "视频"
					}, {
						"id" : 2,
						"text" : "音频"
					}, {
						"id" : 3,
						"text" : "文本"
					} ],
					onChange : function(newValue, oldValue) {
						if (newValue && newValue != '0') {
							categoryckeyQue.combobox('reload', baseUrl
									+ '/category/list/top/' + newValue);
						} else {
							categoryckeyQue.combobox('options').data = [ {
								ckey : '',
								text : '全部分类集合',
								selected : true
							} ];
						}

						categoryListDatagridReload();
					}
				});

		categoryListDatagrid = $('#categoryList').datagrid(
				{
					url : '${baseUrl}/category/list',
					pagination : true,
					sortName : 'sort,updateTime,ckey',
					sortOrder : 'desc,desc,asc',
					multiSort : true,
					//remoteSort : false,
					// 			rownumbers : true,

					queryParams : categoryQueryParams,
					singleSelect : true,
					idField : 'id',
					striped : true,
					pageSize : 20,
					fitColumns : true,
					toolbar : '#categoryListToolBars',
					onLoadSuccess : function() {
						categorySelectIndex = undefined;
						categorySelectRow = undefined;
						categoryEditIndex = undefined;
						categoryEditRow = undefined;
					},
					onBeforeSelect : function(index, row) {
						if (categoryEditIndex != undefined) {
							return false;
						} else {
							categorySelectIndex = index;
							categorySelectRow = row;
						}
					},
					onBeforeEdit : function(index, row) {
						if (categoryEditIndex == undefined) {
							categoryEditIndex = index;
							categoryEditRow = row;
						}
					},
					onEndEdit : function(index, row, changes) {
					},
					// 			nowrap : false,
					columns : [ [
							{
								field : 'id',
								title : 'ID',
								align : 'center',
								width : 60
							},
							{
								field : 'text',
								title : '名称',
								width : 249,
								editor : {
									type : 'text',
									options : {
										required : true
									}
								}
							},
							{
								field : 'ckey',
								title : '类型KEY',
								align : 'center',
								width : 100,
								sortable : true
							},
							{
								field : 'itemTypeMsg',
								title : '资源类型',
								align : 'center',
								width : 100
							},
							{
								field : 'createTime',
								title : '创建时间',
								width : 100,
								align : 'left'
							},
							{
								field : 'updateTime',
								title : '更新时间',
								width : 100,
								align : 'left',
								sortable : true
							},
							{
								field : 'createUser',
								title : '创建者',
								width : 60,
								align : 'center',
								formatter : function(value, row, index) {
									if (row.createUser) {
										return row.createUser.nickname;
									} else {
										return value;
									}
								}
							},
							{
								field : 'entity',
								title : '分类实体',
								width : 80,
								align : 'center',
								formatter : function(value, row, index) {
									return value;
								}
							},
							{
								field : 'parent',
								title : '所属分类集合',
								width : 249,
								align : 'left',
								formatter : function(value, row, index) {
									if (row.parent) {
										return "<span>集合ID:" + row.parent.id
												+ "</span><br/><span>集合标题:"
												+ row.parent.text + "</span>";
									} else {
										return value;
									}
								}
							}, {
								field : 'sort',
								title : '排序',
								width : 60,
								align : 'center',
								sortable : true,
								editor : {
									type : 'numberbox',
									options : {
										min : 0,
										precision : 2,
										required : true
									}
								}
							} ] ],
					onDblClickRow : function(index, row) {
						$.ajax({
							data : {
								id : row.id
							},
							async : false,
							url : baseUrl + '/category/info.html',
							success : function(html) {
								$('body').append(html);
							}
						});

					}
				});

		categoryListDatagrid.datagrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							categoryListDatagrid.datagrid('gotoPage',
									categoryListDatagrid.datagrid('getPager')
											.find('.pagination-num').val());
						}
					} ]
				});

	});

	function categoryList_append() {

		$.ajax({
			url : baseUrl + '/category/add.html?entity=true',
			success : function(html) {
				$('body').append(html);
			}
		});
	}
	function categoryList_edit() {

		var categorySelect = categoryListDatagrid.datagrid('getSelected');
		if (categoryEditIndex != undefined
				&& categoryEditIndex != categorySelectIndex) {
			$.messager.alert('提示', '请完成已开启编辑行', 'warning');
			return;
		}

		if (categorySelect) {
			if (categorySelectIndex == undefined) {
				categorySelectIndex = categoryListDatagrid.datagrid(
						"getRowIndex", categorySelect);
			}

			categoryListDatagrid.datagrid('beginEdit', categorySelectIndex);
			categorySelectRow = categorySelect;
		}
	}

	function categoryList_undo() {
		if (categorySelectIndex != undefined) {
			categoryListDatagrid.datagrid('rejectChanges');
			categorySelectIndex = undefined;
			categorySelectRow = undefined;
			categoryEditIndex = undefined;
			categoryEditRow = undefined;
		}
	}

	function categoryList_accept() {
		if (categorySelectIndex != undefined) {
			if (categoryListDatagrid.datagrid('validateRow',
					categorySelectIndex)) {

				//验证名称
				var _edTitle = categoryListDatagrid.datagrid('getEditor', {
					index : categorySelectIndex,
					field : 'text'
				});

				var _Title = $(_edTitle.target).val();
				if (!_Title || _Title.trim() == '') {
					$.messager.alert('提示', '名称不能为空', 'warning');
					return;
				}

				//获取排序
				var _edSort = categoryListDatagrid.datagrid('getEditor', {
					index : categorySelectIndex,
					field : 'sort'
				});

				var _Sort = $(_edSort.target).val();

				var _Data = {
					id : categorySelectRow.id,
					name : _Title,
					sort : _Sort
				};

				$.ajax({
					data : _Data,
					async : false,
					type : 'POST',
					url : baseUrl + '/category/update',
					success : function(data) {
						if (data.responseCode == '000') {
							categoryListDatagrid.datagrid('endEdit',
									categorySelectIndex);

							categoryListDatagrid.datagrid('acceptChanges');

							categorySelectIndex = undefined;
							categorySelectRow = undefined;
							categoryEditIndex = undefined;
							categoryEditRow = undefined;

							$.messager.alert('提示', '修改成功', 'info');
						} else {
							$.messager.alert('提示', data.responseMessage,
									'error');
						}
					}
				});

			}
		}
	}

	function categoryList_categoryListPage() {
		var categorySelect = categoryListDatagrid.datagrid('getSelected');
		if (categorySelect) {
			$.ajax({
				data : {
					itemId : categorySelect.id,
					itemType : 1
				},
				async : false,
				url : baseUrl + '/category/set.html',
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}

	function categoryList_tagListPage() {

		var categorySelect = categoryListDatagrid.datagrid('getSelected');
		if (categorySelect) {
			$.ajax({
				data : {
					itemId : categorySelect.id,
					itemType : 1
				},
				async : false,
				url : baseUrl + '/tag/set.html',
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}

	function categoryList_remove() {
		var categorySelect = categoryListDatagrid.datagrid('getSelected');
		if (categorySelect) {
			$.messager.confirm('确认', '您确认要删除么吗？', function(r) {
				if (r) {
					categoryList_remove2(categorySelect, false);
				}
			});
		}
	}

	function categoryList_remove2(categorySelect, force) {
		$.ajax({
			data : {
				ids : [ categorySelect.id ],
				force : force,
				entity : true
			},
			type : "post",
			async : false,
			url : baseUrl + '/category/delete',
			success : function(data) {
				if (data.responseCode == '000') {
					$.messager.alert('提示', "删除成功!", 'info');

					categoryListDatagrid.datagrid('deleteRow',
							categoryListDatagrid.datagrid('getRowIndex',
									categorySelect));

				} else if (data.responseCode == '023') {
					$.messager.confirm('确认强制删除',
							'<span style="color:red;">强制删除会解除所有关联资源关系</span>',
							function(fr) {
								if (fr) {
									categoryList_remove2(categorySelect, true);
								}
							})
				} else {
					$.messager.alert('提示', data.responseMessage, 'error');
				}
			}
		});
	}
</script>
